
const fileMqttMixin = {
  data () {
    return {
      // mqtt实例
      requestId: '', // 唯一的userId用于连接盒子，此id必须唯一
      client: null,
      boxId: '', // 当前订阅的boxId
      programId: '', // 传输文件，当前订阅的requestId
      programId1: '' // 传输文件，当前订阅的requestId
    }
  },
  mounted () {
    this.initMqtt()
  },
  methods: {
    // 初始化mqtt
    initMqtt () {
      // 生成一个唯一的userId用于连接盒子，此id必须唯一
      this.requestId = 'webClient' + this.userInfo.user_id.toString() + Date.parse(new Date()).toString()
      // 创建mq实例
      this.client = new window.Paho.MQTT.Client(
        this.CommonConfig.mqttConfig.MQTT_SERVICE,
        Number(this.CommonConfig.mqttConfig.MQRR_PORT),
        this.requestId)
      // 连接服务器并注册连接成功处理事件
      let self = this
      this.client.connect({
        keepAliveInterval: 30,
        onSuccess: this.onConnect,
        onFailure: function (message) { // 连接失败
          console.log('连接失败', JSON.stringify(message))
          // 3秒后重连
          setTimeout(() => {
            self.initMqtt()
          }, 3000)
        }
      })
      // 注册连接断开处理事件
      this.client.onConnectionLost = this.onConnectionLost
      // 注册消息接收处理事件
      this.client.onMessageArrived = this.onMessageArrived
    },
    onConnect () {
      console.log('注册连接成功')
      this.onSubscribeAll()
      // this.onSubscribeAll2()
    },
    // 订阅并发送消息,订阅主题是获取盒子列表，删除，重命名，内部存储发送结果
    onSubscribe (boxId) {
      if (!this.boxId || this.boxId.length === 0) {
        this.boxId = boxId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${this.boxId}`,
          { qos: 2 }
        )
      } else if (this.boxId !== boxId) {
        // 取消订阅
        // this.client.unsubscribe(
        //   `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${this.boxId}`,
        //   { qos: 2 }
        // )
        this.boxId = boxId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${this.boxId}`,
          { qos: 2 }
        )
      }
    },
    unsubscribe (boxId) {
      this.client.unsubscribe(
        `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${boxId}`
      )
    },
    // 订阅并发送消息,订阅主题是获取上传进度
    onSubscribe1 (programId) {
      console.log(this.programId, programId)
      // if (this.programId.length === 0 || this.programId !== programId) {
      //   this.programId = programId
      //   // 订阅主题
      //   this.client.subscribe(
      //     `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendInfo.subscribeTopic}${this.programId}`,
      //     { qos: 2 }
      //   )
      //   console.log(`我订阅的是${this.programId}`)
      // }
      if (this.programId.length === 0) {
        this.programId = programId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendInfo.subscribeTopic}${this.programId}`,
          { qos: 2 }
        )
      } else if (this.programId !== programId) {
        // 取消订阅
        this.client.unsubscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${this.programId}`
        )
        this.programId = programId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendInfo.subscribeTopic}${this.programId}`,
          { qos: 2 }
        )
      }
    },
    // 订阅并发送消息,订阅主题是外部存储发送结果
    onSubscribe2 (programId) {
      if (this.programId1.length === 0 || this.programId1 !== programId) {
        this.programId1 = programId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendResult.subscribeTopic}${this.programId1}`,
          { qos: 2 }
        )
      }
    },
    // 链接断开重连时，重新订阅
    onSubscribeAll () {
      console.log('重新连接成功')
      if (this.boxId.length > 0) {
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}${this.boxId}`,
          { qos: 2 }
        )
      }
      if (this.programId.length > 0) {
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendInfo.subscribeTopic}${this.programId}`,
          { qos: 2 }
        )
      }
      if (this.programId1.length > 0) {
        this.client.subscribe(
          `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendResult.subscribeTopic}${this.programId1}`,
          { qos: 2 }
        )
      }
    },
    // 所有
    onSubscribeAll2  () {
      console.log('重新连接成功')
      this.client.subscribe(
        `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileInfo.subscribeTopic}#`,
        { qos: 2 }
      )
      this.client.subscribe(
        `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendInfo.subscribeTopic}#`,
        { qos: 2 }
      )
      this.client.subscribe(
        `${this.CommonConfig.mqttConfig.MQTT_TOPIC.machineFileSendResult.subscribeTopic}#`,
        { qos: 2 }
      )
    },
    onMessageArrived (message) {
      console.log(message)
    },
    // 连接丢失
    onConnectionLost (responseObject) {
      console.log('连接断开事件')
      if (responseObject.errorCode !== 0) {
        console.log('onConnectionLost:' + responseObject.errorMessage)
        console.log('连接已断开')
      }
      // 断开重连
      // this.client.connect({
      //   keepAliveInterval: 30,
      //   onSuccess: this.onSubscribeAll
      // })
      let self = this
      setTimeout(() => {
        self.initMqtt()
      }, 3000)
    }
  }
}

export default fileMqttMixin
